package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.accounting.entity.GaProductSummaryTransition;
import com.jrx.anytxn.accounting.service.IProductSummaryService;
import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmAccountMainInfo;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-parent
 * @description: 客户批量-产品金额汇总(日终批量)
 * @author: xiaoyu.jing
 * @date: 2020-09-02 10:00
 **/
@Order(70)
@Service
public class ProductSummaryOnlineHandle extends BaseBatchAccoutHandle<GaProductSummaryTransition> {

    Logger logger = LoggerFactory.getLogger(ProductSummaryOnlineHandle.class);

    @Autowired
    private IProductSummaryService productSummaryService;
    @Autowired
    private RedisTemplate<String, Object> template;


    @Override
    public List<GaProductSummaryTransition> handBatchAccout() throws Exception {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        List<AmLoanAccountInfo> dealLoanAccountList = custBatchSaveBean.getDealLoanAccountList();
        if(CollectionUtils.isEmpty(dealLoanAccountList)){
            return null;
        }

        List<GaProductSummaryTransition> list = new ArrayList<>();
        List<AmAccountMainInfo> dealAcctList = custBatchSaveBean.getDealAcctList();
        for(AmLoanAccountInfo loanAcct : dealLoanAccountList) {
            String loanId = loanAcct.getLoanId();
            List<AmAccountMainInfo> accountList = null;
            if(CollectionUtils.isNotEmpty(dealAcctList)){
                accountList = dealAcctList.stream().filter(acct -> loanId.equals(acct.getLoanId())).collect(Collectors.toList());
            }
            GaProductSummaryTransition transition = productSummaryService.summary(loanAcct, accountList);
            if(null != transition){
                list.add(transition);
            }
        }
        logger.info("ProductSummaryOnlineHandle用户:{},需要处理的订单：{}",
                custBatchSaveBean.getDealCust().getCustomerId(),
                custBatchSaveBean.getDealLoanAccountList().stream().map(AmLoanAccountInfo::getLoanId).collect(Collectors.joining(","))
        );
        return list;
    }

    @Override
    public void afterHandle(List<GaProductSummaryTransition> list) {
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        list.forEach(this::putTransition);
    }

    /**
     * 获取，修改缓存map加锁
     *
     * @param transition
     */
    private void putTransition(GaProductSummaryTransition transition) {
        String key = String.format(TransBizConstant.PRODUCT_SUMMARY_LOAN_REDIS_KEY,transition.getTenantId(), transition.getLoanId());
        ValueOperations<String, Object> redis = template.opsForValue();
        // SET key value: 设置指定 key 的值
        redis.set(key, transition,24L, TimeUnit.HOURS);
    }

    @Override
    public String getBatchType() {
        return TransBizConstant.BATCH_TYPE_2;
    }
}
